<?php
include('admin.php');
set_page_info('id', 'settings');
set_page_info('title', 'Editing Settings');

if (isset($_POST['submit'])) {
	$c = get_post_data();
	$results = array();
	
	$c['index_pagination'] = (int)$c['index_pagination'];
	if ($c['index_pagination'] <= 0) $c['index_pagination'] = 3;
	$c['archive_pagination'] = (int)$c['archive_pagination'];
	if ($c['archive_pagination'] <= 0) $c['archive_pagination'] = 10;
	
	if (empty($c['website']) || empty($c['install_path'])) error('Please enter the site and install path.');
	if (empty($c['site_name'])) $c['site_name'] = 'Articulate Articles';
	$c['website'] = add_trailing_slash($c['website']);
	$c['install_path'] = remove_leading_slash($c['install_path']);
	
	if (!empty($c['pretty_permalinks'])) $c['pretty_permalinks'] = 1;
	else $c['pretty_permalinks'] = 0;
	
	if (!empty($c['email_comments'])) $c['email_comments'] = 1;
	else $c['email_comments'] = 0;
	
	write_htaccess($c['pretty_permalinks']);
	
	unset($c['submit']);
	foreach($c as $key => $value) {
		$results[] = $mysql->update(SETTINGS, array(
			'setting_value' => $value
		), '`setting_key` = "'.$key.'"');
	}
	
	if (in_array(false, $results, true)) error('There was an error saving the settings.');
	else header('Location: settings.php?msg=save');
}

get_header();
?>
		<h2>Editing Settings</h2>
<?php if (isset($_GET['msg']) && $_GET['msg'] == 'save') { ?>
		<p class="success">The settings have been saved.</p>
<?php } ?>
		<form action="settings.php" method="post">
			<p><label for="website">Website</label>
			<input type="text" name="website" id="website" value="<?php echo get_option('website'); ?>" />
			<span class="hint">Please include trailing slash!</span></p>
			
			<p><label for="install_path">Install URL</label>
			<input type="text" name="install_path" id="install_path" value="<?php echo get_option('install_path'); ?>" />
			<span class="hint">No leading slash!</span></p>
			
			<p><label for="site_name">Site Name</label>
			<input type="text" name="site_name" id="site_name" value="<?php echo get_option('site_name'); ?>" /></p>
			
			<p><label for="index_pagination">Time Offset</label>
			<input type="text" name="timeoffset" id="timeoffset" value="<?php echo get_option('timeoffset'); ?>" />
			<span class="hint">Current time with offset - <?php echo get_date(false, mysql_result($mysql->query('SELECT NOW()'), 0), true); ?></span></p>
			
			<p><label for="index_pagination">Index Pagination</label>
			<input type="text" name="index_pagination" id="index_pagination" value="<?php echo get_option('index_pagination'); ?>" /></p>
			
			<p><label for="archive_pagination">Archive Pagination</label>
			<input type="text" name="archive_pagination" id="archive_pagination" value="<?php echo get_option('archive_pagination'); ?>" /></p>
			
			<p>
				<label>Pretty Permalinks</label>
				<input type="radio" name="pretty_permalinks" id="pretty_permalinks[0]" class="radio" value="1"<?php if (get_option('pretty_permalinks')) echo ' checked="checked"'; ?> /> <label for="pretty_permalinks[0]" class="radiolabel">Yes</label>
				<input type="radio" name="pretty_permalinks" id="pretty_permalinks[1]" class="radio" value="0"<?php if (!get_option('pretty_permalinks')) echo ' checked="checked"'; ?> /> <label for="pretty_permalinks[1]" class="radiolabel">No</label>
			</p>
			
			<p>
				<label>Email New Comments</label>
				<input type="radio" name="email_comments" id="email_comments[0]" class="radio" value="1"<?php if (get_option('email_comments')) echo ' checked="checked"'; ?> /> <label for="email_comments[0]" class="radiolabel">Yes</label>
				<input type="radio" name="email_comments" id="email_comments[1]" class="radio" value="0"<?php if (!get_option('email_comments')) echo ' checked="checked"'; ?> /> <label for="email_comments[1]" class="radiolabel">No</label>
			</p>
			
			<p><input type="submit" name="submit" class="button" value="Save" /></p>
		</form>
<?php get_footer(); ?>